import sys
import traceback

from xjlibrary.all_step.BaseAllStep import GlobalControl
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
print(curPath)
sys.path.insert(0, curPath)  # 插入

from Step1_downlist import main as step1_main
from Step2_downjournals import main as step2_main
from Step3_parajournal import main as step3_main
from Step4_downvol import main as step4_main
from Step5_paravol import main as step5_main
from Step6_downissue import main as step6_main
from Step7_paraissue import main as step7_main
from Step8_downarticle import main as step8_main
from Step9_downcover import main as step9_main
from Step10_createjson import main as step10_main
from Step11_mapcover import main as step11_main


class UseGlobalControl(GlobalControl):

    def __init__(self):
        super().__init__(__file__, BaseDir.get_new_path(BaseDir.get_file_dir_absolute(__file__), "logs"))

    def imports(self):
        # import_module("Step1_save_url_periodical.main")
        # import_module("Step2_save_time_list.main")
        # import_module("Step3_parse_timedetails.main")
        # import_module("Step4_save_issue_list.main")
        # import_module("Step5_parse_issue_fulltext.main")
        # import_module("Step6_save_fulltext_list.main")
        # import_module("Step7_parse_fulltext_list.main")
        # import_module("Step8_Update_cover.main")
        # import_module("Step9_down_cover.main")
        pass

    def init(self):
        sql = "update journal set cover_stat = 0 where cover_stat=1"
        self.mysqlutils.ExeSqlToDB(sql)
        super().init()

    def run(self, strings):
        """
        该步骤运行代码
        :return:
        """
        try:
            if strings == "":
                return True
            result = eval(strings)
        except:
            print(traceback.format_exc())
            return False
        return result

    def start_hook(self, key):
        if key == "11":
            self.key = "11"
            self.nowstep = ""
            return True

    def one_check(self):
        """
        该步骤检查代码是否运行完成
        :return:
        """
        if self.key == "1":
            self.writetrue()
        elif self.key == "2":
            self.writetrue()
        elif self.key == "3":
            self.writetrue()
        elif self.key == "4":
            self.writetrue()
        elif self.key == "5":
            self.writetrue()
        elif self.key == "6":
            self.writetrue()
        elif self.key == "7":
            self.writetrue()
        elif self.key == "8":
            self.writetrue()
        elif self.key == "9":
            self.writetrue()
        elif self.key == "10":
            self.writetrue()
        elif self.key == "11":
            self.writetrue()

        if "11" in self.jsondicts.keys() and self.jsondicts["11"]["isFinish"]:
            return True
        return False


if __name__ == "__main__":
    globalcontrol = UseGlobalControl()
    globalcontrol.work()
